// Keepfit/pages/meInfo/meInfo.js
const db = wx.cloud.database({
  throwOnNotFound: false
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:"",
    userSex:"男",
    userLocation:"",
    userPhoneNumber:"",
    registerTime:"",
    showSexslecet:false,
    radio: '1',
    phoneStatus:"",
    checkPhone: false,
  },
  add0: function(m) {
    return m < 10 ? '0' + m : m
  },

  format: function(timeStamp) {
    //timeStamp是整数，否则要parseInt转换

    if(typeof(timeStamp) != typeof(666))
    {
      timeStamp = parseInt(timeStamp)
    }
    var time = new Date(timeStamp);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return y + '-' + this.add0(m) + '-' + this.add0(d)
  },


  getUserInfo(){
    const ui = wx.getStorageSync("userInfo")
    db.collection("User").where({
      openid:ui.openid
    }).get().then(res => {
      
      let username=res.data[0].name
      let userSex =res.data[0].sex
      let userLocation = res.data[0].city
      let userPhoneNumber =res.data[0].phoneNumber
      let regi = res.data[0].registerTime
      console.log(regi)

      regi = this.format(regi)
      this.setData({
        username: username,
        userSex:userSex,
        userLocation:userLocation,
        userPhoneNumber:userPhoneNumber,
        registerTime:regi
      })

      
    })
  },
  updateInfo(){
    const ui = wx.getStorageSync("userInfo")
    db.collection("User").where({
      openid:ui.openid
    }).update({
      data: {
        sex: this.data.userSex,
        name:this.data.username,
        phoneNumber:this.data.userPhoneNumber
      },
    }).then(res =>{
      const ui = wx.getStorageSync("userInfo")
      ui.nickName = this.data.username
      wx.setStorageSync("userInfo", ui )
    })
  },
  checkPhoneNumber(e){

    let value = parseInt(e.detail)
    console.log(value)
    if(value<9999999999 || 100000000000<value){
      if(!this.data.checkPhone){
        this.setData({
          checkPhone:true
        })
      }
    }else{
      this.setData({
        checkPhone:false
      })
    }  
  },
  checkPhoneStatus(e){
    
    if(!this.data.checkPhone){
      db.collection("User").where({
        phoneNumber:e.detail.value
      }).get().then(res =>{
        if(res.data.length !=0){
          this.setData({
            phoneStatus:"手机号不可用",
            checkPhone:true
          })
        }else{
          this.setData({
            userPhoneNumber:e.detail.value,
            phoneStatus:"",
          })
        }
      })
    }
    
  },
  //返回me
  onClickLeft() {
    wx.showToast({ title: '点击返回', icon: 'none' });
    wx.navigateBack({
      delta: 1
    });
    this.updateInfo()
  },

  onClose(){
    this.setData({
      showSexslecet:false,
    })
  },
  sexSlecet(){
    this.setData({
      showSexslecet:true,
    })
  },

  onChange(event) {
    if(event.detail == "1"){
      this.setData({
        radio: event.detail,
        userSex:"男"
      });
    }else{
      this.setData({
        radio: event.detail,
        userSex:"女"
      });
    }
    
  },
  editName(e){
    console.log(e)
    this.setData({
      username:e.detail.value
    })
  },

  onClick(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio: name,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.updateInfo()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})